package cn.zshprint.serein.controller;

import cn.zshprint.serein.pojo.EsProduct;
import cn.zshprint.serein.pojo.ResultBean;
import cn.zshprint.serein.service.EsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * es产品控制器
 *
 * @author zshprint
 * @date 2021/07/29
 */
@RestController
@RequestMapping("/searchProduct")
public class EsProductController {
    @Autowired
    private EsProductService esProductService;

    /**
     * 导入所有
     *
     * @return {@link ResultBean<Integer>}
     */
    @PostMapping("importAll")
    public ResultBean<Integer> importAll() {
        int count = esProductService.importAll();
        return new ResultBean<>(count);
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link ResultBean<Object>}
     */
    @DeleteMapping("delete/{id}")
    public ResultBean<Object> delete(@PathVariable Long id) {
        esProductService.delete(id);
        return new ResultBean<>(null);
    }

    /**
     * 删除批处理
     *
     * @param ids id
     * @return {@link ResultBean<Object>}
     */
    @DeleteMapping("delete/batch")
    public ResultBean<Object> deleteBatch(@RequestBody List<Long> ids) {
        esProductService.delete(ids);
        return new ResultBean<>(null);
    }

    /**
     * 搜索
     *
     * @param keyword 关键字
     * @return {@link ResultBean<List<EsProduct>>}
     */
    @GetMapping("search/{keyword}")
    public ResultBean<List<EsProduct>> search(@PathVariable String keyword) {
        List<EsProduct> search = esProductService.search(keyword);
        return new ResultBean<>(search);
    }


    @GetMapping("searchAll")
    public ResultBean<List<EsProduct>> searchAll() {
        List<EsProduct> esProductList = esProductService.searchAll();
        return new ResultBean<>(esProductList);
    }

}
